package com.vonchenchen.pulldownmenu.UI;

import android.content.Context;
import android.text.method.CharacterPickerDialog;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.vonchenchen.pulldownmenu.MyApplication;
import com.vonchenchen.pulldownmenu.R;

import java.util.List;

/**
 * Created by vonchenchen on 2015/10/30 0030.
 */
public class DoubleListViewHolder extends BaseHolder<List<List<String>>> {

    private List<List<String>> mData;

    private ListView mLeftListView;
    private ListView mRightListView;

    private List<String> mLeftList;
    private List<String> mRightList;
    private TextListAdapter mLeftAdapter;
    private TextRightListAdapter mRightAdapter;

    private View mViewClickRecorder = null;
    private boolean mFirstMesure = true;

    @Override
    public View initView() {
        View view = View.inflate(MyApplication.getContext(), R.layout.doublelistview_layout, null);

        mLeftListView = (ListView) view.findViewById(R.id.ll_left);
        mRightListView = (ListView) view.findViewById(R.id.ll_right);



        mLeftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //如果点击条目，更换被点击条目的颜色
                if(mViewClickRecorder != view){
                    view.setBackgroundColor(MyApplication.getContext().getResources().getColor(R.color.normal_selected_color));
                    if(mViewClickRecorder != null) {
                        mViewClickRecorder.setBackgroundColor(MyApplication.getContext().getResources().getColor(R.color.normal_unselected_color));
                    }
                   mViewClickRecorder = view;
                }

                mRightList = mData.get(position+1);

                //mRightAdapter.notifyDataSetChanged();

                mRightAdapter = new TextRightListAdapter(mRightList);
                mRightListView.setAdapter(mRightAdapter);
            }
        });

        return view;
    }

    @Override
    public void refreshView(List<List<String>> info) {

        this.mData = info;

        mLeftList = info.get(0);
        mLeftAdapter = new TextListAdapter(mLeftList);

        mRightList = info.get(1);
        mRightAdapter = new TextRightListAdapter(mRightList);

        mLeftListView.setAdapter(mLeftAdapter);
        mRightListView.setAdapter(mRightAdapter);
    }

    class TextListAdapter extends MyAdapter{

        public TextListAdapter(List list) {
            super(list);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            TextViewHolder holder = null;
            if(convertView == null){
                holder = new TextViewHolder();
            }else{
                holder = (TextViewHolder) convertView.getTag();
            }

            convertView = holder.getView();
            holder.refreshView((String) getItem(position));

            //防止多次测量
            if(position == 0 && mFirstMesure){
                mFirstMesure = false;
                convertView.setBackgroundColor(MyApplication.getContext().getResources().getColor(R.color.normal_selected_color));
                mViewClickRecorder = convertView;
            }

            return convertView;
        }
    }

    class TextRightListAdapter extends MyAdapter{

        public TextRightListAdapter(List list) {
            super(list);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            TextViewHolder holder = null;
            if(convertView == null){
                holder = new TextViewHolder();
            }else{
                holder = (TextViewHolder) convertView.getTag();
            }

            convertView = holder.getView();
            holder.refreshView((String) getItem(position));

//            if(position == 0){
//                convertView.setBackgroundColor(MyApplication.getContext().getResources().getColor(R.color.normal_selected_color));
//                mViewClickRecorder = convertView;
//            }

            return convertView;
        }
    }

    /**
     * ListView 中的 Holder
     */
    private class TextViewHolder extends BaseHolder<String>{

        private TextView mTextView;

        @Override
        public View initView() {
            View view = View.inflate(MyApplication.getContext(), R.layout.simpletext_item, null);
            mTextView = (TextView) view.findViewById(R.id.tv_text);
            return view;
        }

        @Override
        public void refreshView(String info) {
            mTextView.setText(info);
        }
    }
}
